package cn.uncode.dal.utils;

import java.util.concurrent.atomic.AtomicInteger;

public class IDGenerateUtils {

	public final static long BASE_TIME = 1422766646843L;
	
	private static volatile AtomicInteger SEQUENCE_ATOMIC = new AtomicInteger((int) System.currentTimeMillis() % 10000);

	/**
	 * 生成唯一ID
	 * 
	 * @throws Exception
	 * @author meff
	 */
	public static long getId() {
		long time = System.currentTimeMillis() - BASE_TIME;
		if (SEQUENCE_ATOMIC.incrementAndGet() >= 9999) {
			synchronized(SEQUENCE_ATOMIC){
				if (SEQUENCE_ATOMIC.get() >= 9999) {
					SEQUENCE_ATOMIC.set(1000);
				}
			}
			SEQUENCE_ATOMIC.incrementAndGet();
		}
		time = time * 10000 + SEQUENCE_ATOMIC.get();
		return time;
	}
	
	
	public static void main(String[] args) {
		System.out.println(IDGenerateUtils.getId());
		
	}
}
